package com.foreveross.crawl.common.core;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/** 
 * @ClassName: ProxyIp 
 * @Description: TODO
 * @author luofangyi
 * @date 2014-10-15 下午5:24:45 
 *  
 */ 
@Entity
@Table(name = "PROXY_IP")
@Cacheable
public class ProxyIpEntity extends DateIntervalEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4932779208166383055L;
	private String ip;
	private Set<ProxyChannelRelateEntity> proxyChannelRelates = new HashSet<ProxyChannelRelateEntity>(0);
	
	@Override
	public String[] businessKeys() {
		
		return new String[]{this.getId().toString(), this.getIp()};
	}

//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = super.hashCode();
//		result = prime * result + ((ip == null) ? 0 : ip.hashCode());
//		result = prime
//				* result
//				+ ((proxyChannelRelates == null) ? 0 : proxyChannelRelates
//						.hashCode());
//		return result;
//	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProxyIpEntity other = (ProxyIpEntity) obj;
		if (ip == null) {
			if (other.ip != null)
				return false;
		} else if (!ip.equals(other.ip))
			return false;
		if (proxyChannelRelates == null) {
			if (other.proxyChannelRelates != null)
				return false;
		} else if (!proxyChannelRelates.equals(other.proxyChannelRelates))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ProxyIpEntity [ip=" + ip + ", proxyChannelRelates="
				+ proxyChannelRelates + "]";
	}

	public static void saveOrUpdate(ProxyIpEntity entity){
		getRepository().save(entity);
	}
	
	public static void deleteAll(){
		getRepository().createJpqlQuery("delete from ProxyIpEntity").executeUpdate();
	}
	
	public static <T extends org.dayatang.domain.Entity> T load(Class<T> clazz, Serializable id) {
		return getRepository().load(clazz, id);
	}
	
	public static boolean isProxyIpExisted(String ip){
		return !getRepository().createJpqlQuery("select t from ProxyIpEntity t where t.ip=:ip").addParameter("ip", ip).list().isEmpty();
	}
	
	public static ProxyIpEntity findProxyIpByIp(String ip){
		List<ProxyIpEntity> list = findByProperty(ProxyIpEntity.class, "ip", ip);
		return (list != null && list.size() != 0) ? list.get(0) : null;
	}

	@Column(name = "IP")
	public String getIp() {
		return this.ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "proxyIp")
	public Set<ProxyChannelRelateEntity> getProxyChannelRelates() {
		return this.proxyChannelRelates;
	}

	public void setProxyChannelRelates(
			Set<ProxyChannelRelateEntity> proxyChannelRelates) {
		this.proxyChannelRelates = proxyChannelRelates;
	}

}